using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UI_QuestSlot : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI questName;
    [SerializeField] private Image[] rewardPreview;
    private UI_QuestPreview questPreview;
    private UI_Quest questUI;

    private QuestDataSO questDataInSlot;


    public void SetupQuestSlot(QuestDataSO questDataSO)
    {
        questUI = transform.root.GetComponent<UI>().questUI;

        questPreview = transform.root.GetComponentInChildren<UI_QuestPreview>();

        questDataInSlot = questDataSO;

        questName.text = questDataSO.questName;

        foreach (var image in rewardPreview)
            image.gameObject.SetActive(false);

        for (int i = 0; i < questDataSO.rewardItems.Length; i++)
        {
            if (questDataSO.rewardItems == null || questDataSO.rewardItems[i].itemData == null)
                continue;

            rewardPreview[i].sprite = questDataSO.rewardItems[i].itemData.itemIcon;
            rewardPreview[i].GetComponentInChildren<TextMeshProUGUI>().text = questDataSO.rewardItems[i].stackSize.ToString();
            rewardPreview[i].gameObject.SetActive(true);
        }
        
        UpdateSlotBorder();
    }
    public void UpdatePreview()
    {
        questPreview.SetupQuestPreview(questDataInSlot);
    }
    //判断是否接受了当前卡槽的任务
    public bool IsAcceptedQuest() => questUI.questManager.IsAcceptedQuest(questDataInSlot);

    public void UpdateSlotBorder()
    {
        Image borderImage = transform.GetComponent<Image>();
        if (IsAcceptedQuest())
            borderImage.color = new(0, 175, 255, 255);
        else
            borderImage.color = Color.white;
    }
}
